package switchviz.graphic.signal;

import java.util.StringTokenizer;

import org.jdom.Element;

public class BasicSignalPersistence 
{
	private final static String SIGNAME = "name";
	public final static String SIGTYPE = "type";
	private final static String SIGCOLOR = "color";
	private final static String MIN = "minvalue";
	private final static String MAX = "maxvalue";
	private final static String ALARMMODE = "alarmmode";
	private final static String PRIORITY = "priority";
	
	private BasicSignalDataModel _datamodel = null;
	protected Element _root = null;
	
	public Element getElement()
	{
		return _root;
	}
	
	public BasicSignalDataModel getDataModelFromElement()
	{
		getDataModelFromElementBasic();
		return getDataModel();
	}
	
	protected void getDataModelFromElementBasic()
	{
		if(_root.getChild(SIGNAME) != null)
			getDataModel().setName(_root.getChildText(SIGNAME));
		if(_root.getChild(SIGTYPE) != null)
			getDataModel().setType(_root.getChildText(SIGTYPE));
		if(_root.getChild(SIGCOLOR) != null)
			getDataModel().setColor(getColorCode(_root.getChildText(SIGCOLOR)));
		if(_root.getChild(MIN) != null)
			getDataModel().setMin(Float.parseFloat(_root.getChildText(MIN)));
		if(_root.getChild(MAX) != null)
			getDataModel().setMax(Float.parseFloat(_root.getChildText(MAX)));
		if(_root.getChild(ALARMMODE) != null)
			getDataModel().setAlarmmode(Integer.parseInt(_root.getChildText(ALARMMODE)));
		if(_root.getChild(PRIORITY) != null)
			getDataModel().setPriority(Integer.parseInt(_root.getChildText(PRIORITY)));
	}
	
	protected void saveDataModelBasic()
	{
		addNode(SIGNAME, getDataModel().getName());
		addNode(SIGTYPE, getDataModel().getType());
		addNode(MIN, String.valueOf(getDataModel().getMin()));
		addNode(MAX, String.valueOf(getDataModel().getMax()));
		addNode(ALARMMODE, String.valueOf(getDataModel().getAlarmmode()));
		addNode(PRIORITY, String.valueOf(getDataModel().getPriority()));
		String colorcode = getDataModel().getColor()[0] + ";" + getDataModel().getColor()[1] + ";" + getDataModel().getColor()[2];
		addNode(SIGCOLOR, colorcode);
	}
	
	public Element setDataModel(BasicSignalDataModel datamodel)
	{
		_datamodel = datamodel;
		return saveDataModel();
	}
	
	public BasicSignalPersistence(Element root)
	{
		_root = root;
	}
	
	private float[] getColorCode(String colorcode)
	{
		float[] color = {0.0f, 0.0f, 0.0f};
		StringTokenizer tokenizer = new StringTokenizer(colorcode, ";", false);
		int i = 0;
		while(tokenizer.hasMoreTokens())
		{
			if(i == 3) break;
			color[i] = Float.valueOf(tokenizer.nextToken());
			++i;
		}
		return color;
	}
	
	protected BasicSignalDataModel getDataModel()
	{
		if (_datamodel == null) 
			_datamodel = new BasicSignalDataModel();
		return _datamodel;
	}
	
	protected void addNode(String nodename, String value)
	{
		if(_root.getChild(nodename) != null)
			_root.getChild(nodename).setText(value);
		else
			_root.addContent(new Element(nodename).setText(value));
	}
	
	protected Element saveDataModel()
	{
		saveDataModelBasic();
		return _root;
	}
}
